package com.controller;

import com.entity.CourseType;
import com.github.pagehelper.PageInfo;
import com.service.CourseTypeService;
import com.util.AjaxResult;
import org.aspectj.weaver.loadtime.Aj;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 陈忠杰
 * @Date: 2021/07/05/11:36
 * @Description:
 */
@RestController
@RequestMapping("ct")
public class CourseTypeController {

    @Autowired
    CourseTypeService courseTypeService;

    @RequestMapping("addBackType.do")
    public AjaxResult addBackType(CourseType courseType){
        AjaxResult ajaxResult = new AjaxResult();
        int i = courseTypeService.addCourseType(courseType);
        if (i==0){
            ajaxResult.setMsg("类别名称重复，添加失败！请重新添加！");
        }else {
            ajaxResult.setMsg("添加成功！");
        }
        return ajaxResult;
    }


    @RequestMapping("findBackCourseType.do")
    public PageInfo<CourseType> findBackCourseType(int parent_id,int pageNo){
        PageInfo<CourseType> courseTypeByParent_idAndPageNo = courseTypeService.getCourseTypeByParent_idAndPageNo(parent_id, pageNo);
        return courseTypeByParent_idAndPageNo;

    }

    @RequestMapping("findBackCourseTypeZi.do")
    public List<CourseType> findBackCourseType(int parent_id){
        List<CourseType> courseTypeByParent_id = courseTypeService.getCourseTypeByParent_id(parent_id);
        return courseTypeByParent_id;
    }

    @RequestMapping("showBackName.do")
    public AjaxResult showBackName(int id){
        AjaxResult ajaxResult = new AjaxResult();
        CourseType courseTypeById = courseTypeService.getCourseTypeById(id);
        ajaxResult.setObj(courseTypeById);
        return ajaxResult;
    }

    @RequestMapping("showParent_id.do")
    public AjaxResult showParent_id(){
        AjaxResult ajaxResult = new AjaxResult();
        List<CourseType> allCourseType = courseTypeService.getAllCourseType();
        ajaxResult.setObj(allCourseType);
        return ajaxResult;
    }

    @RequestMapping("toggleStatus.do")
    public int toggleStatus(int id,int status){
        int i = courseTypeService.changeStatus(id, status);
        return i;
    }

    @RequestMapping("modifyCourseTypeName.do")
    public AjaxResult modifyCourseTypeName(CourseType courseType){
        AjaxResult ajaxResult = new AjaxResult();
        int i = courseTypeService.changeCourseTypeByInfo(courseType);
        ajaxResult.setObj(i);
        if (i!=0) {
            ajaxResult.setMsg("更新成功！");
        }else {
            ajaxResult.setMsg("更新失败，您要更新的类型名重复，请重新输入！");
        }
        return ajaxResult;
    }
}
